/*
 * Copyright (c) 2009, Jan Pastor. All Rights Reserved.
 */

package sk.jp.jitag.ui;

import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Date;
import javax.swing.*;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import sk.jp.imageio.jpeg.JpegImage;
import sk.jp.imageio.jpeg.JpegImageIo;
import sk.jp.imageio.jpeg.JpegImageIoImpl;
import sk.jp.imageio.jpeg.metadata.Info;
import sk.jp.jitag.ConfigManager;
import sk.jp.jitag.vo.file.ImageFile;
import sk.jp.jitag.vo.file.JpegImageFile;
import sk.jp.jitag.ui.thumb.FileThumbnail;
import sk.jp.jitag.ui.thumb.JpegImageThumbnail;
import sk.jp.jitag.ui.thumb.ThumbnailListMouseEvent;
import sk.jp.jitag.ui.thumb.ThumbnailListRenderer;

/**
 * Panel that presents a given set of media files as thumbnails.
 */
@Component
public class FileThumbnailsPanel extends JPanel {

    private static final Logger log = LoggerFactory.getLogger(MainWindow.class);

    @Autowired
    private ConfigManager configManager;

    @Autowired
    private MainWindow mainWindow;

    //----------------------------------------------------------------------

    public FileThumbnailsPanel() {
        init();
//        initTest();//TODO test
    }

    //----------------------------------------------------------------------

    //----------------------------------------------------------------------

    private void init(){
        setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));

        final JList thumbnailList = new JList();
        thumbnailList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        thumbnailList.setLayoutOrientation(JList.HORIZONTAL_WRAP);
        thumbnailList.setVisibleRowCount(-1);
        thumbnailList.setCellRenderer(new ThumbnailListRenderer());
        thumbnailList.setBorder(null);
        thumbnailList.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent event) {
                //Translate event to list item component
                Point clickPoint = event.getPoint();
                int index = thumbnailList.locationToIndex(clickPoint);
                ListCellRenderer renderer = thumbnailList.getCellRenderer();
                Rectangle cellBounds;

                if(index != -1 && renderer != null){
                    cellBounds = thumbnailList.getCellBounds(index, index);
                    if(cellBounds != null && cellBounds.contains(clickPoint.x, clickPoint.y)){
                        Object itemAtIndex = thumbnailList.getModel().getElementAt(index);
                        boolean itemHasFocus = thumbnailList.hasFocus() && (thumbnailList.getLeadSelectionIndex() == index);
                        java.awt.Component component = renderer.getListCellRendererComponent(
                                                        thumbnailList,
                                                        itemAtIndex,
                                                        index,
                                                        thumbnailList.isSelectedIndex(index),
                                                        itemHasFocus);

                        if(component instanceof JComponent) {
                            MouseEvent newEvent;

                            clickPoint.translate(-cellBounds.x, -cellBounds.y);
                            Point absolute = new Point(thumbnailList.getLocationOnScreen());
                            absolute. translate(event.getX(), event.getY());
                            newEvent = new ThumbnailListMouseEvent(
                                                      component, event.getID(),
                                                      event.getWhen(),
                                                      event.getModifiers(),
                                                      clickPoint.x, clickPoint.y,
                                                      event.getClickCount(),
                                                      event.isPopupTrigger(),
                                                      absolute);
                            component.dispatchEvent(newEvent);
                        }
                    }
                }
            }
        });

        JScrollPane scrollPane = new JScrollPane(thumbnailList);
        add(scrollPane);
        scrollPane.setBorder(null);

        DefaultListModel listModel = new DefaultListModel();

        //
        //TODO test
        String basePath;
        basePath = System.getProperty("user.home")+"/thumbtest/"; //win
        try {
            int thumbnailSize = 5*16+8+16;
            JpegImageIo imageIo = new JpegImageIoImpl();
            JpegImage jpegImage2;
            FileThumbnail thumbnail;

//        jpegImage2 = imageIo.loadImage(new File("e:\\projekty\\jitag\\_temp\\testdata\\thumbtest\\IMG_6112.jpg"));
//        jpegImage2 = imageIo.loadImage(new File("/media/HOME/projekty/jitag/_temp/testdata/thumbtest/IMG_6112.jpg"));
//        basePath = "thumbtest/";//linux
            jpegImage2 = imageIo.loadImage(new File(basePath+"IMG_6112.jpg"));
            thumbnail = new JpegImageThumbnail(new JpegImageFile(jpegImage2), thumbnailSize);
            listModel.addElement(thumbnail);

            jpegImage2 = imageIo.loadImage(new File(basePath+"IMG_6113.jpg"));
            thumbnail = new JpegImageThumbnail(new JpegImageFile(jpegImage2), thumbnailSize);
            listModel.addElement(thumbnail);

            jpegImage2 = imageIo.loadImage(new File(basePath+"IMG_6114.jpg"));
            thumbnail = new JpegImageThumbnail(new JpegImageFile(jpegImage2), thumbnailSize);
            listModel.addElement(thumbnail);

            jpegImage2 = imageIo.loadImage(new File(basePath+"IMG_6115.jpg"));
            thumbnail = new JpegImageThumbnail(new JpegImageFile(jpegImage2), thumbnailSize);
            listModel.addElement(thumbnail);

            jpegImage2 = imageIo.loadImage(new File(basePath+"IMG_6120.jpg"));
            thumbnail = new JpegImageThumbnail(new JpegImageFile(jpegImage2), thumbnailSize);
            listModel.addElement(thumbnail);

            ImageFile imageFile = new ImageFile(new Info(basePath+"music.png", "", new Date()));
            thumbnail = new FileThumbnail(imageFile, thumbnailSize);
            listModel.addElement(thumbnail);

            thumbnailList.setModel(listModel);
        } catch (Exception e) {
            log.error("Demo image files not found or can not be read from location '"+basePath+"'", e);
        }
    }

}
